var ChannelInfoDlg = {channelInfoData: {}, zTreeInstance: null, validateFields: {}};

ChannelInfoDlg.clearData = function () {
    this.channelInfoData = {};
};

ChannelInfoDlg.set = function (key, val) {
    this.channelInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};

ChannelInfoDlg.get = function (key) {
    return $("#" + key).val();
};

ChannelInfoDlg.close = function () {
    parent.layer.close(window.parent.Channel.layerIndex);
};

ChannelInfoDlg.onClickChannel = function (e, treeId, treeNode) {
    $("#cName").attr("value", ChannelInfoDlg.zTreeInstance.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
};

ChannelInfoDlg.showChannelSelectTree = function () {
    var cName = $("#cName");
    var cNameOffset = $("#cName").offset();
    $("#parentChannelMenu").css({
        left: cNameOffset.left + "px",
        top: cNameOffset.top + cName.outerHeight() + "px"
    }).slideDown("fast");
    $("body").bind("mousedown", onBodyDown);
};

ChannelInfoDlg.hideChannelSelectTree = function () {
    $("#parentChannelMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

ChannelInfoDlg.collectData = function () {
    this.set('id').set('channelname').set('remarks').set('num').set('pid');
};

ChannelInfoDlg.validate = function () {
    $('#channelInfoForm').data("bootstrapValidator").resetForm();
    $('#channelInfoForm').bootstrapValidator('validate');
    return $("#channelInfoForm").data('bootstrapValidator').isValid();
};

ChannelInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/channel/add", function (data) {
        Feng.success("添加成功!");
        window.parent.Channel.table.refresh();
        ChannelInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.channelInfoData);
    ajax.start();
};

ChannelInfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/channel/update", function (data) {
        Feng.success("修改成功!");
        window.parent.Channel.table.refresh();
        ChannelInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.channelInfoData);
    ajax.start();
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentChannelMenu" || $(event.target).parents("#parentChannelMenu").length > 0)) {
        ChannelInfoDlg.hideChannelSelectTree();
    }
};

ChannelInfoDlg.editMaxLevelSubmit = function () {
    var id = document.getElementById("id").value;
    var maxLevel = document.getElementById("maxLevel").value;
    var remarks = document.getElementById("remarks").value;
    $.ajax({
        url: Feng.ctxPath + "/channel/editMaxLevelSubmit",
        type: "post",
        data: {"id": id, "maxLevel": maxLevel, "remarks": remarks},
        success: function (data) {
            Feng.success("修改成功!");
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        },
        error: function (data) {
            alert('没有权限，请联系管理员!');
        }
    });
};

$(function () {
    Feng.initValidator("channelInfoForm", ChannelInfoDlg.validateFields);
    var ztree = new $ZTree("parentChannelMenuTree", "/channel/tree");
    ztree.bindOnClick(ChannelInfoDlg.onClickChannel);
    ztree.init();
    ChannelInfoDlg.zTreeInstance = ztree;
});